﻿using System;
using System.Web.Http;
using System.Web.Http.Tracing;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using TfsStatus.Infrastructure;

namespace TfsStatus
{
	public class MvcApplication : System.Web.HttpApplication
	{
		protected void Application_Start()
		{
			AreaRegistration.RegisterAllAreas();

			FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
			RouteConfig.RegisterRoutes(RouteTable.Routes);
			BundleConfig.RegisterBundles(BundleTable.Bundles);
			
			ConfigureWebApi();
		}

		protected void Application_Error()
		{
			Exception lastException = Server.GetLastError();
			NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
			logger.Fatal(lastException);
		}

		private void ConfigureWebApi()
		{
			// NLog Tracewriter
			GlobalConfiguration.Configuration.Services.Replace(typeof(ITraceWriter), new NLogTraceWriter());

			// JSON.NET Serializer
			JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
			serializerSettings.Converters.Add(new IsoDateTimeConverter());
			GlobalConfiguration.Configuration.Formatters[0] = new JsonNetFormatter(serializerSettings);
		}
	}
}